package com.liu.efk.service;

import com.liu.efk.domain.*;
import com.liu.efk.dto.CodeProjectDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shi860715@126.com
 * Date 2021/3/5 10:14
 * 操作缓存
 */
@Service
@Slf4j
public class RedisService {

    @Autowired
    RedisTemplate redisTemplate;



    @Autowired
    ICodeProject1Service codeProject1Service;
    @Autowired
    ICodeProject2Service codeProject2Service;
    @Autowired
    ICodeProject3Service codeProject3Service;
    @Autowired
    ICodeProject4Service codeProject4Service;


    @Autowired
    IRoleService roleService;

    @Autowired
    IDuserInfoService duserInfoService;





    public List<CodeProjectDto> setRedisCodeProject(){
       List<CodeProject1> codeProject1s = codeProject1Service.list();
       List<CodeProject2> codeProject2s = codeProject2Service.list();
       List<CodeProject3> codeProject3s = codeProject3Service.list();
       List<CodeProject4> codeProject4s = codeProject4Service.list();

       List<CodeProjectDto> list1 = new ArrayList<CodeProjectDto>();
        codeProject1s.forEach(e->{
           CodeProjectDto  dto = new CodeProjectDto();
            BeanUtils.copyProperties(e,dto);
            list1.add(dto);

        });
        codeProject2s.forEach(e->{
            CodeProjectDto  dto = new CodeProjectDto();
            BeanUtils.copyProperties(e,dto);
            list1.add(dto);

        });
        codeProject3s.forEach(e->{
            CodeProjectDto  dto = new CodeProjectDto();
            BeanUtils.copyProperties(e,dto);
            list1.add(dto);

        });
        codeProject4s.forEach(e->{
            CodeProjectDto  dto = new CodeProjectDto();
            BeanUtils.copyProperties(e,dto);
            list1.add(dto);

        });

        list1.forEach(e->{
            redisTemplate.opsForValue().set("project_"+e.getCode(),e.getValue());

        });

        return list1;

    }



    public List<Drole> setRedisROles(){

       List<Drole> list =     roleService.list();

       list.forEach(e->{
           redisTemplate.opsForValue().set("role_"+e.getRoleId(),e.getName());
       });

       return list;
    }


    public List<DuserInfo> setRedisUserInfo(){
        List<DuserInfo> list = duserInfoService.list();
        list.forEach(e->{

            String username =e.getName();
            String key="users_"+e.getMobilephone();




            redisTemplate.opsForValue().set(key,username);


        });
      return list;
    }


}
